home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / pasledit.zip / STRINGS.PAS < prev    next >
Pascal/Delphi Source File  |  1991-02-12  |  8KB  |  266 lines

  1. (* 
  2.  
  3.    Library of string-handling routines written in Standard (ANSI) Pascal.
  4.  
  5.    By Ilya Shlyakhter, D-block.
  6.  
  7. *)
  8.  
  9. UNIT Strings;
  10.  
  11.                               INTERFACE
  12.  
  13.    CONST MaxStrLength = 6;   (* Maximum possible string length *)
  14.  
  15.    TYPE  StrLengthType = 0..MaxStrLength;
  16.          StrCharData = PACKED ARRAY [1..MaxStrLength] OF Char;
  17.  
  18.          StrType = RECORD
  19.                      Length: StrLengthType;
  20.                      Ch: StrCharData
  21.                    END;    (* StrType *)
  22.  
  23.    PROCEDURE StrInit (VAR Target: StrType);
  24.    FUNCTION StrLength (TheString: StrType): StrLengthType;
  25.  
  26.    PROCEDURE StrAddChar (VAR Target: StrType; CharToAdd: Char);
  27.    PROCEDURE StrInsertChar (VAR Target: StrType; CharToInsert: Char; Position: StrLengthType);
  28.  
  29.    PROCEDURE StrDeleteCharPos (VAR Target: StrType; Position: StrLengthType);
  30.    PROCEDURE StrDeleteCharFirst (VAR Target: StrType; Character: Char);
  31.  
  32.    PROCEDURE StrConcat (First, Second: StrType; VAR Target: StrType);
  33.  
  34.    PROCEDURE StrReplaceCharPos (VAR Target: StrType; NewChar: Char; Position: StrLengthType);
  35.    PROCEDURE StrReplaceCharFirst (VAR Target: StrType; OldChar: Char; NewChar: Char);
  36.    PROCEDURE StrReplaceCharAll (VAR Target: StrType; OldChar: Char; NewChar: Char);
  37.  
  38.    FUNCTION StrCharPos (SearchString: StrType; SearchChar: Char): StrLengthType;
  39.    PROCEDURE StrDisplayString (TheString: StrType);
  40.  
  41.    FUNCTION StrEqual (First, Second: StrType):Boolean;
  42.  
  43.  
  44.                             IMPLEMENTATION
  45.  
  46.  
  47.    PROCEDURE StrInit (VAR Target: StrType);
  48.  
  49.    (*
  50.        Initializes the given string (Target) by setting its length to zero.
  51.    *)
  52.  
  53.       BEGIN  (* StrInit *)
  54.          Target.Length := 0
  55.       END;   (* StrInit *)
  56.  
  57.    FUNCTION StrLength (TheString: StrType): StrLengthType;
  58.  
  59.    (*
  60.        Returns the length of a given string (TheString).
  61.    *)
  62.  
  63.       BEGIN  (* StrLength *)
  64.         StrLength := TheString.Length
  65.       END;   (* StrLength *)
  66.  
  67.  
  68.    FUNCTION StrCharPos (SearchString: StrType; SearchChar: Char): StrLengthType;
  69.  
  70.    (*
  71.       Returns the first occurence of a character (SearchChar) in a string (SearchString).
  72.       If the character is not found, returns zero.
  73.    *)
  74.  
  75.       VAR I: StrLengthType;
  76.           Result: StrLengthType;  (* value to be returned by the function *)
  77.  
  78.       BEGIN  (* StrCharPos *)
  79.          I := 1;
  80.  
  81.          WHILE (SearchString.Ch [I] <> SearchChar) AND (I <= SearchString.Length) DO
  82.             I := I + 1;
  83.  
  84.          IF SearchString.Ch [I] = SearchChar THEN
  85.             Result := I     (* found *)
  86.                ELSE
  87.                   Result := 0;   (* character not found *)
  88.  
  89.          StrCharPos := Result
  90.       END;   (* StrCharPos *)
  91.  
  92.    PROCEDURE StrAddChar (VAR Target: StrType; CharToAdd: Char);
  93.  
  94.    (*
  95.        Appends character (CharToAdd) to the string (Target).
  96.    *)
  97.  
  98.       BEGIN  (* StrAddChar *)
  99.          IF Target.Length < MaxStrLength THEN
  100.             BEGIN  (* append *)
  101.               Target.Length := Target.Length + 1;
  102.               Target.Ch [Target.Length] := CharToAdd
  103.             END;   (* append *)
  104.  
  105.       END;   (* StrAddChar *)
  106.  
  107.    PROCEDURE StrInsertChar (VAR Target: StrType; CharToInsert: Char; Position: StrLengthType);
  108.  
  109.    (*
  110.        Inserts a character (CharToInsert) into a string (Target) at the specified position (Position).
  111.    *)
  112.  
  113.       VAR I: StrLengthType;
  114.  
  115.       BEGIN  (* StrInsertChar *)
  116.          IF Target.Length < MaxStrLength THEN
  117.             BEGIN  (* insert *)
  118.                FOR I := Target.Length+1 DOWNTO Position+1 DO
  119.                   Target.Ch [I] := Target.Ch [I-1];
  120.  
  121.                Target.Ch [Position] := CharToInsert;
  122.                Target.Length := Target.Length + 1;
  123.             END;   (* insert *)
  124.       END;   (* StrInsertChar *)
  125.  
  126.    PROCEDURE StrDeleteCharPos (VAR Target: StrType; Position: StrLengthType);
  127.  
  128.    (*
  129.        Deletes a single character from a string (Target) at the specified position (Position).
  130.    *)
  131.  
  132.       VAR I: StrLengthType;
  133.  
  134.       BEGIN  (* StrDeleteCharPos *)
  135.         IF Target.Length > 0 THEN
  136.           BEGIN  (* delete *)
  137.             FOR I := Position TO Target.Length DO
  138.                Target.Ch [I] := Target.Ch [I+1];
  139.  
  140.             Target.Length := Target.Length - 1
  141.           END;   (* delete *)
  142.       END;   (* StrDeleteCharPos *)
  143.  
  144.    PROCEDURE StrDeleteCharFirst (VAR Target: StrType; Character: Char);
  145.  
  146.    (*
  147.       Deletes the first occurence of a character (Character) in a string (Target).
  148.  
  149.    *)
  150.  
  151.       VAR CharPos: StrLengthType;
  152.  
  153.       BEGIN  (* StrDeleteCharFirst *)
  154.          CharPos := StrCharPos (Target, Character);
  155.  
  156.          IF CharPos > 0 THEN     (* the character was found in the string *)
  157.             StrDeleteCharPos (Target, CharPos);
  158.       END;   (* StrDeleteCharFirst *)
  159.  
  160.  
  161.    PROCEDURE StrConcat (First, Second: StrType; VAR Target: StrType);
  162.  
  163.    (*
  164.       Concatenates (merges) two given strings (First and Second) and stores result
  165.       in another string (Target).
  166.    *)
  167.  
  168.       VAR I: StrLengthType;
  169.  
  170.       BEGIN  (* StrConcat *)
  171.          IF (First.Length + Second.Length) > MaxStrLength THEN
  172.             Second.Length := MaxStrLength - First.Length;
  173.  
  174.          Target := First;
  175.          FOR I := 1 TO Second.Length DO
  176.             Target.Ch [First.Length + I] := Second.Ch [I];
  177.  
  178.          Target.Length := First.Length + Second.Length
  179.       END;   (* StrConcat *)
  180.  
  181.    PROCEDURE StrReplaceCharPos (VAR Target: StrType; NewChar: Char; Position: StrLengthType);
  182.  
  183.    (*
  184.       Replaces a character in the string (Target) at the specified POSITION (Position)
  185.       with a given character (NewChar).
  186.    *)
  187.  
  188.       BEGIN  (* StrReplaceCharPos *)
  189.          Target.Ch [Position] := NewChar
  190.       END;   (* StrReplaceCharPos *)
  191.  
  192.    PROCEDURE StrReplaceCharFirst (VAR Target: StrType; OldChar: Char; NewChar: Char);
  193.  
  194.    (*
  195.       Replaces the FIRST occurence of a character (OldChar) in a string (Target)
  196.       with a given character (NewChar).
  197.    *)
  198.  
  199.       VAR CharPos: StrLengthType;
  200.  
  201.       BEGIN  (* StrReplaceCharFirst *)
  202.  
  203.          CharPos := StrCharPos (Target, OldChar);
  204.  
  205.          IF CharPos > 0 THEN  (* character found in the string *)
  206.             Target.Ch [CharPos] := NewChar;
  207.       END;   (* StrReplaceCharFirst *)
  208.  
  209.    PROCEDURE StrReplaceCharAll (VAR Target: StrType; OldChar: Char; NewChar: Char);
  210.  
  211.    (*
  212.       Replaces ALL occurences of a character (OldChar) in a string (Target) 
  213.       with a given character (NewChar).
  214.    *)
  215.  
  216.       VAR I: StrLengthType;
  217.  
  218.       BEGIN  (* StrReplaceCharAll *)
  219.          FOR I := 1 TO Target.Length DO
  220.             IF Target.Ch [I] = OldChar THEN
  221.                Target.Ch [I] := NewChar
  222.       END;   (* StrReplaceCharAll *)
  223.  
  224.    FUNCTION StrEqual (First, Second: StrType): Boolean;
  225.  
  226.       VAR I: Integer;
  227.           Result: Boolean;
  228.  
  229.       BEGIN  (* StrEqual *)
  230.          IF First.Length <> Second.Length THEN
  231.             Result := False  (* if the strings have different lengths, they can't be equal *)
  232.                ELSE
  233.                   BEGIN  (* the two strings have the same lengths *)
  234.                      I := 1;
  235.  
  236.                      WHILE (I < MaxStrLength) AND (First.Ch [I] = Second.Ch [I]) DO
  237.                         I := I+1;
  238.  
  239.                      Result := (I = MaxStrLength) AND (First.Ch [I] = Second.Ch [I])
  240.  
  241.                   END;   (* the two strings have the same lengths *)
  242.  
  243.          StrEqual := Result
  244.  
  245.       END;   (* StrEqual *)
  246.  
  247.  
  248.    PROCEDURE StrDisplayString (TheString: StrType);
  249.  
  250.       VAR I: StrLengthType;
  251.  
  252.       BEGIN  (* StrDisplayString *)
  253.          FOR I := 1 TO StrLength (TheString) DO
  254.             Write (TheString.Ch [I]);
  255.  
  256.          WriteLn
  257.       END;   (* StrDisplayString *)
  258.  
  259.  
  260.  
  261.    END.   (* of Unit Strings *)
  262.  
  263.  
  264.  
  265.  
  266.